package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.views.IncomeDayReport;
import com.xmy.cultivate.entity.views.ReNewDetailForTeacher;
import com.xmy.cultivate.service.IRenewDataService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.ConfigData;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author min
 * @since 2022-11-29
 */
@RestController
@RequestMapping("/api/renewData")
@Scope("prototype")
public class RenewDataController {

    @Autowired
    IRenewDataService iRenewDataService;

    /**
     * 续班详情
     */
    @GetMapping("detailForTeacher")
    public Result detailForTeacher(@RequestParam Map reMap){

        Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 20: Integer.valueOf((String)reMap.get("pageSize"));

        String yearPart = reMap.get("yearPart").toString();
        String subjectsId = reMap.get("subjectsId").toString();
        String quarterNum = reMap.get("quarterNum").toString();
        String teacherId = reMap.get("teacherId").toString();
        String schoolId = reMap.get("schoolId").toString();

        String isRenew = (String) reMap.get("isRenew");

        String gradeId = (String) reMap.get("gradeId");

        Map<String,Integer> nextQuarter = ConfigData.getNextQuarter(Integer.parseInt(yearPart),Integer.parseInt(quarterNum));

        QueryWrapper<ReNewDetailForTeacher> queryWrapper =  new QueryWrapper<>();

        queryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part_renew",nextQuarter.get("yearPart"));
        queryWrapper.eq(StringUtils.isNotBlank(quarterNum),"quarter_num_renew",nextQuarter.get("num"));
        queryWrapper.eq(StringUtils.isNotBlank(subjectsId),"subjects_id",subjectsId);
        queryWrapper.eq(StringUtils.isNotBlank(teacherId),"teacher_id",teacherId);

        queryWrapper.eq("type",1);
        queryWrapper.eq("level",1);
        queryWrapper.isNotNull("year_class_id");
        //renewDataQueryWrapper.inSql("student_id","select student_id from student_grade as b left join grade as c on b.grade_id = c.id where c.teacher_id="+renewClassRecord.getTeacherId()+" and b.deleted=0 and b.status<>2 and b.reading_status=1 and b.subjects_id="+SubjectsId+" and b.year_part="+YearPart+" and b.quarter_num="+QuarterNum+"");
        if(quarterNum.equals(2)||quarterNum.equals(3)){
            queryWrapper.lt("year_class_id",9);
        }else {
            queryWrapper.le("year_class_id",9);
        }

        queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);

        if(StringUtils.isNotBlank(gradeId)){
            queryWrapper.inSql("student_id","select student_id from student_grade as a where a.`status`=1 and a.reading_status not in(2,3) and grade_id = "+gradeId+"");
        }

        if(StringUtils.isNotBlank(isRenew)){
            if(isRenew.equals("1")){
                queryWrapper.eq("is_renew",isRenew);
                queryWrapper.eq("true_renew",1);
            }else{
                //queryWrapper.eq("is_renew",isRenew);
                queryWrapper.eq("true_renew",0);
            }

        }

        //queryWrapper.eq("arrive_affirm",2);
        queryWrapper.orderByDesc("created_at");

        IPage<IncomeDayReport> iPage = new Page<>(pageNum,pageSize);

        iRenewDataService.getRnewDetailForTeahcer(iPage,queryWrapper);

        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }
}
